package com.LeeCode;

import java.util.ArrayList;
import java.util.List;

/**
 * 恢复二叉搜索树
 */

public class Code099 {
    public static void main(String[] args) {

    }

    private TreeNode node1, node2, prev;

    public void recoverTree(TreeNode root) {
        inorder(root);
        int temp = node1.val;
        node1.val = node2.val;
        node2.val = temp;
    }

    private void inorder(TreeNode root) {
        if (root == null) return;
        inorder(root.left);
        if (prev != null && prev.val > root.val) {
            if (node1 == null) node1 = prev;
            node2 = root;
        }
        prev = root;
        inorder(root.right);
    }
}
